﻿// ===============================================================================
// BaseGenerator.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Data.Generator
{
    using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.IO;
    using EOS.Framework.Tools.Aspects;

    /// <summary>
    /// Baseclass for all code generators.
    /// </summary>
    public class BaseGenerator
    {
        /// <summary>
        /// Code DOM definition for the namespace.
        /// </summary>
        private CodeNamespace targetNamespace;

        /// <summary>
        /// Gets the Code DOM definition for the target namespace.
        /// </summary>
        /// <value>The target namespace.</value>
        protected CodeNamespace TargetNamespace
        {
            get { return targetNamespace; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseGenerator"/> class.
        /// </summary>
        /// <param name="targetNamespace">The target namespace. Cannot be null (Nothing in Visual), empty
        /// or contains only blanks.</param>
        /// <exception cref="ArgumentNullException"><paramref name="targetNamespace"/> is null (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="targetNamespace"/> is a string of length 0 or contains only blanks.</exception>
        [ParamNotNullOrEmpty("targetNamespace")]
        public BaseGenerator(string targetNamespace)
        {
            this.targetNamespace = new CodeNamespace(targetNamespace);
        }

        /// <summary>
        /// Saves the code unit to the file with the specified path.
        /// </summary>
        /// <param name="path">The path where to save the code unit to.</param>
        /// <exception cref="ArgumentNullException"><paramref name="targetNamespace"/> is
        /// null (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="targetNamespace"/> is a string
        /// with length 0 or contains only blanks.</exception>
        [ParamNotNullOrEmpty("path")]
        public void SaveToFile(string path)
        {
            CodeCompileUnit compileUnit = new CodeCompileUnit();
            compileUnit.Namespaces.Add(targetNamespace);

            CodeDomProvider csharpProvider = CodeDomProvider.CreateProvider("CSharp");
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.VerbatimOrder = true;
            options.BracingStyle = "C";

            using (StreamWriter sourceWriter = new StreamWriter(path))
            {
                csharpProvider.GenerateCodeFromCompileUnit(compileUnit, sourceWriter, options);
            }

        }
    }
}
